//
//  LinkReaderTableView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/4.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "LinkReaderTableView.h"
#import "LinkReaderTableViewCell.h"
#import "ReaderModel.h"
#import "MJRefresh.h"
#import "NetEaseDataService.h"
#import "NewsDetailViewController.h"
#import "ReaderViewController.h"

@implementation LinkReaderTableView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.delegate = self;
        self.dataSource = self;
        
        [self addRefreshAction];
        
        [self registerClass:[LinkReaderTableViewCell class] forCellReuseIdentifier:kLinkReaderTableViewCellId];
    }
    
    return self;
}

- (void)setReaderModelArray:(NSArray *)readerModelArray {
    if (_readerModelArray != readerModelArray) {
        _readerModelArray = [readerModelArray copy];
        
        [self reloadData];
    }
}

#pragma mark - refreshAction
- (void)addRefreshAction {
    //下拉事件,刷新最新数据
    self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewestAction)];
    
    //上拉事件，在原基础上再刷新一波
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreAction)];
}

- (void)loadNewestAction {
    [NetEaseDataService readerRequestSize:20 block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"阅读页 推荐阅读 数据请求失败 %@", result);
            [self.header endRefreshing];
            return;
        }
        NSArray* array = [result objectForKey:@"推荐"];
        if (array.count) {
            self.readerModelArray = array;
        }
    }];
    [self.header endRefreshing];
}

- (void)loadMoreAction {
    [NetEaseDataService readerRequestSize:20 block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"阅读页 推荐阅读 数据请求失败 %@", result);
            [self.footer endRefreshing];
            return;
        }
//        NSLog(@"%@", [Tools getJsonStrFromObj:result]);
        NSArray* array = [result objectForKey:@"推荐"];
        if (array.count) {
            NSMutableArray* newArray = [NSMutableArray arrayWithArray:self.readerModelArray];
            [newArray addObjectsFromArray:array];
            self.readerModelArray = newArray;
        }
    }];
    [self.footer endRefreshing];
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _readerModelArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 10)];
    view.backgroundColor = [UIColor grayColor];
    view.alpha = 0.3;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ReaderModel* model = [[ReaderModel alloc] initWithDataDic:_readerModelArray[indexPath.section]];
    CGFloat imgWidth = (kWidth-20)/3;
    if (model.extraImgArray.count == 2) {            //三张图片
        return 50+imgWidth*2+20+10;
    } else {                //一张图片
        return 20+imgWidth+10;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LinkReaderTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kLinkReaderTableViewCellId forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    ReaderModel* model = [[ReaderModel alloc] initWithDataDic:_readerModelArray[indexPath.section]];
    cell.readerModel = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //通过响应者链,找到readerVC
    ReaderViewController* readerVC = [Tools className:@"ReaderViewController" fromResponder:self.nextResponder];
    
    if (readerVC) {
        ReaderModel* model = [[ReaderModel alloc] initWithDataDic:_readerModelArray[indexPath.section]];
        
        NewsDetailViewController* newsDetailVC = [[NewsDetailViewController alloc] init];
        if (model.readerId.length) {
            newsDetailVC.newsId = model.readerId;
            newsDetailVC.replyId = model.replyId;
            [readerVC.navigationController pushViewController:newsDetailVC animated:YES];
        }
    }
}



@end
